package com.tfleo.mealssenger.adapter;

import java.util.List;

import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.object.Hangout;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class HangoutListAdapter extends ArrayAdapter<Hangout> {
	private LayoutInflater mInflater;
	
	public HangoutListAdapter(Context context, int resource, int textViewResourceId, 
			List<Hangout> objects, LayoutInflater inflater) {
		super(context, resource, textViewResourceId, objects);
		mInflater = inflater;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {   
		ViewHolder holder = null;
		TextView location = null;
		TextView numOfGuests = null;
		TextView date = null;
		
		final Hangout hangout = getItem(position);
		
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.hangout_entry, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		}

		holder = (ViewHolder) convertView.getTag();
		
		location = holder.getLocationView();
		location.setText(hangout.placeName);
		
		numOfGuests = holder.getNumOfGuestsView();
		numOfGuests.setText(Integer.toString(hangout.numOfGuests) + " people invited");
		
		date = holder.getDateView();
		date.setText(hangout.date);

		return convertView;
	}
	
	private class ViewHolder {
		private View vRow;
		private TextView vLocation = null;
		private TextView vNumOfGuests = null;
		private TextView vDate = null;

		public ViewHolder(View row) {
			vRow = row;
		}

		public TextView getLocationView() {
			if (vLocation == null) {
				vLocation = (TextView) vRow.findViewById(R.id.location);
			}
			return vLocation;
		}
		
		public TextView getNumOfGuestsView() {
			if(vNumOfGuests == null) {
				vNumOfGuests = (TextView) vRow.findViewById(R.id.num_guests);
			}
			return vNumOfGuests;
		}

		public TextView getDateView() {
			if (vDate == null) {
				vDate = (TextView) vRow.findViewById(R.id.date);
			}
			return vDate;
		}
	}
}
